﻿#region Header Licence
//  NKnobMan, Copyright 2009, Alexandre MUTEL.
// 
//  This is free software; you can redistribute it and/or modify it
//  under the terms of the GNU Lesser General Public License as
//  published by the Free Software Foundation; either version 2.1 of
//  the License, or (at your option) any later version.
// 
//  This software is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//  Lesser General Public License for more details.
// 
//  You should have received a copy of the GNU Lesser General Public
//  License along with this software; if not, write to the Free
//  Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
//  02110-1301 USA, or see the FSF site: http://www.fsf.org.
#endregion
using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace NKnobMan
{
    public partial class KnobManTraceListenerPanel : UserControl
    {
        private PanelTraceListener traceListener;
        public KnobManTraceListenerPanel()
        {
            InitializeComponent();

            Array traceLevels = Enum.GetValues(typeof (TraceLevel));
            foreach (TraceLevel traceLevel in traceLevels)
            {
                comboBoxTraceLevel.Items.Add(traceLevel);
            }
            comboBoxTraceLevel.SelectedItem = Log.TraceSwitch.Level;

            traceListener = new PanelTraceListener(this);

            Trace.Listeners.Add(traceListener);
        }

        public void AppendLog(string value)
        {            
            if (InvokeRequired)
            {
                this.Invoke((MethodInvoker)(() => textBoxLog.AppendText(DateTime.Now + " : " + value)));
                return;
            }
            textBoxLog.AppendText(DateTime.Now + " : " + value);
        }

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            Trace.Listeners.Remove(traceListener);
            base.Dispose(disposing);
        }

        internal class PanelTraceListener : TraceListener
        {
            private KnobManTraceListenerPanel _panel;
            public PanelTraceListener(KnobManTraceListenerPanel panel)
            {
                _panel = panel;
            }
            public override void Write(string message)
            {
                _panel.AppendLog(message);
            }

            public override void WriteLine(string message)
            {
                _panel.AppendLog(message + "\r\n");
            }
        }

        private void comboBoxTraceLevel_SelectedIndexChanged(object sender, EventArgs e)
        {
            Log.TraceSwitch.Level = (TraceLevel)comboBoxTraceLevel.SelectedItem;
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            textBoxLog.Text = "";
        }
    }
}
